import serial
import time
fenx=2400
deny=1080
absx=fenx/2
absy=deny/2
sleepti=0.1
import threading
lock=threading.Lock()
# 串口配置（根据你的设备和需求修改）
ser = serial.Serial(
    port='COM4',  # 串口名称，Windows上是'COMx'，Linux上是'/dev/ttyUSBx'或'/dev/ttyACMx'
    baudrate=115200,  # 波特率
    parity=serial.PARITY_NONE,  # 校验位
    stopbits=serial.STOPBITS_ONE,  # 停止位
    bytesize=serial.EIGHTBITS,  # 数据位
    timeout=1  # 读取超时设置
)
time.sleep(1)
#右移
def right(s):
    s=int(s)

    if s>=0 and s<=127:
        lock.acquire()
        d=bytes([0x57,0xAB,0x00,0x05,0x05,0x01,0x00,0x00])
        num=0
        for a in d:
            num=num+a
        num=num+s

        pi=hex(s)[2:]
        # 十六进制字符串
        hex_string1 = "57 AB 00 05 05 01 00 "+str(pi)+" 00 00 "+str(hex(num&0xff)[2:])

        # 将十六进制字符串转换为字节
        data_to_send = bytes.fromhex(hex_string1)

        # 发送数据
        ser.write(data_to_send)
        time.sleep(sleepti)
        lock.release()
#左移
def left(s):
    s=int(s)
    if s>=0 and s<=127:
        lock.acquire()
        s=255-s
        d=bytes([0x57,0xAB,0x00,0x05,0x05,0x01,0x00,0x00])
        num=0
        for a in d:
            num=num+a
        num=num+s
        print(s)
        pi=hex(s)[2:]

        # 十六进制字符串
        s=str(hex(num&0xff)[2:])
        if len(s)<2:
            s="0"+s
        hex_string1 = "57 AB 00 05 05 01 00 "+str(pi)+" 00 00 "+s

        # 将十六进制字符串转换为字节
        data_to_send = bytes.fromhex(hex_string1)

        # 发送数据
        ser.write(data_to_send)
        time.sleep(sleepti)
        lock.release()
#上移
def up(s):
    s=int(s)

    if s>=0 and s<=127:
        lock.acquire()
        s=255-s
        d=bytes([0x57,0xAB,0x00,0x05,0x05,0x01,0x00,0x00])
        num=0
        for a in d:
            num=num+a
        num=num+s

        pi=hex(s)[2:]
        # 十六进制字符串
        s=str(hex(num&0xff)[2:])
        if len(s)<2:
            s="0"+s
        hex_string1 = "57 AB 00 05 05 01 00 00 " +str(pi)+" 00 "+s

        # 将十六进制字符串转换为字节
        data_to_send = bytes.fromhex(hex_string1)

        # 发送数据
        ser.write(data_to_send)

        time.sleep(sleepti)
        lock.release()
#下移
def down(s):
    s=int(s)

    if s>=0 and s<=127:
        lock.acquire()
        d=bytes([0x57,0xAB,0x00,0x05,0x05,0x01,0x00,0x00])
        num=0
        for a in d:
            num=num+a
        num=num+s

        pi=hex(s)[2:]
        # 十六进制字符串
        s=str(hex(num&0xff)[2:])
        if len(s)<2:
            s="0"+s
        if len(pi)<2:
            pi="0"+str(pi)
        hex_string1 = "57 AB 00 05 05 01 00 00 " +str(pi)+" 00 "+s

        # 将十六进制字符串转换为字节
        data_to_send = bytes.fromhex(hex_string1)

        # 发送数据
        ser.write(data_to_send)
        time.sleep(sleepti)
        lock.release()
#按下左键
def down_left():
    lock.acquire()
    hex_string1 = "57 AB 00 05 05 01 01 00 00 00 0E"
    print(hex_string1)
    # 将十六进制字符串转换为字节
    data_to_send = bytes.fromhex(hex_string1)
    # 发送数据
    ser.write(data_to_send)
    time.sleep(sleepti)
    lock.release()
#释放左键
def rele_left():
    lock.acquire()
    hex_string1 = "57 AB 00 05 05 01 00 00 00 00 0D"

    # 将十六进制字符串转换为字节
    data_to_send = bytes.fromhex(hex_string1)
    # 发送数据
    ser.write(data_to_send)
    time.sleep(sleepti)
    lock.release()
#复位芯片
def reset():
    hex_string1 = "57 AB 00 0F 00 11"

    # 将十六进制字符串转换为字节
    data_to_send = bytes.fromhex(hex_string1)
    # 发送数据
    ser.write(data_to_send)
    time.sleep(sleepti)

#坐标移动
def move(x,y):
    global absy
    global absx
    tempx=x-absx
    tempy=y-absy
    #向右上移动
    if tempx>=0 and tempy<0:
        numx=int(abs(tempx)/100)
        numx_l=tempx%100
        numy=int(abs(tempy)/100)
        numy_l=tempy%100
        while numx>0:
            numx=numx-1
            right(100)
        while numy>0:
            numy=numy-1
            up(100)
        right(numx_l)
        up(numy_l)
        absx=x
        absy=y
        print("右下")

    #向右下
    if tempx>=0 and tempy>=0:
        numx=int(abs(tempx)/100)
        numx_l=tempx%100
        numy=int(abs(tempy)/100)
        numy_l=tempy%100
        while numx>0:
            numx=numx-1
            right(100)
        while numy>0:
            numy=numy-1
            down(100)
        right(numx_l)
        down(numy_l)
        absx=x
        absy=y
        print("向右下")

    #向左上
    if tempx<0 and tempy<0:
        numx=int(abs(tempx)/100)
        numx_l=tempx%100
        numy=int(abs(tempy)/100)
        numy_l=tempy%100
        print(numy)
        print(numx)
        print(numy_l)
        print(numx_l)
        while numx>0:
            numx=numx-1
            left(100)
        while numy>0:
            numy=numy-1
            up(100)
        left(numx_l)
        up(numy_l)
        absx=x
        absy=y
        print("向左上")

    #向左下
    if tempx<0 and tempy>=0:
        numx=int(abs(tempx)/100)
        numx_l=tempx%100
        numy=int(abs(tempy)/100)
        numy_l=tempy%100
        while numx>0:
            numx=numx-1
            left(100)
        while numy>0:
            numy=numy-1
            down(100)
        left(numx_l)
        down(numy_l)
        absx=x
        absy=y
        print("向左下")

def dian(ti):
    down_left()
    time.sleep(ti)
    rele_left()

#right(50)
#left(100)
reset()
time.sleep(2)


move(100,100)
dian(1)
# print(absx)
# print(absy)

# 关闭串口（可选，取决于你的程序是否还需要再次使用串口）
ser.close()